﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using WPLDB.Utility.Extensions;

namespace WPLDB.Infrastucture.IO
{
    public class FileOperations
    {
        #region "Properties"
        private string _tempFoler;

        private string TempFolder
        {
            get
            {
                if (null == _tempFoler)
                    _tempFoler = DateTime.Now.Ticks.ToString();
                return _tempFoler;
            }
        }

        #endregion

        #region "Methods"

        /// <summary>
        /// This method create temporary folder for saving the files
        /// </summary>
        /// <param name="directory"></param>
        /// <returns></returns>
        public string CreateTempFolder(string directory)
        {
            directory = directory + Path.DirectorySeparatorChar.ToString() + TempFolder.ToString().RemoveSpecialChar();

            if (!Directory.Exists(directory))
                directory = Directory.CreateDirectory(directory).FullName;

            return directory;


        }

        /// <summary>
        /// This method deletes temporary folder
        /// </summary>
        /// <param name="directory"></param>
        /// <returns></returns>
        public bool DeleteTempFolder(string directory)
        {
            if (Directory.Exists(directory))
            {
                Directory.Delete(directory);
                return true;
            }


            return false;
        }

        /// <summary>
        /// This method create a .cs file 
        /// </summary>
        /// <param name="fileName">File Name and Path</param>
        /// <param name="content">Content of the file</param>
        /// <param name="isDesigner">True - append Designer word in the file name</param>
        /// <returns></returns>
        public string Write(string fileName, string content, bool isDesigner)
        {

            int i = 0;
            string designer = ".Designer";
            string directory = Path.GetDirectoryName(fileName);
            string fileNameWithOutExtension = Path.GetFileNameWithoutExtension(fileName).Replace(designer, "");
            string extension = Path.GetExtension(fileName);

            while (File.Exists(fileName))
                fileName = string.Format("{0}{3}{1}{4}{2}",
                    directory,
                    fileNameWithOutExtension + ++i,
                    extension,
                    string.IsNullOrWhiteSpace(directory) ? "" : Path.DirectorySeparatorChar.ToString(),
                    isDesigner ? designer : "");

            using (StreamWriter outfile = new StreamWriter(fileName))
            {
                outfile.Write(content);
                outfile.Close();
            }

            return fileName;
        }

        /// <summary>
        /// Delete a file
        /// </summary>
        /// <param name="fileName">File Name and Path</param>
        /// <returns></returns>
        public bool Delete(string fileName)
        {
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
                return true;
            }


            return false;
        } 
        #endregion

    }
}
